package com.graduate.twentyone.mapper;

import com.graduate.twentyone.domain.Sale;
import com.graduate.twentyone.domain.Valume;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface SaleMapper {
    @Insert("insert into sale  values(null,#{num},#{datetime},#{fid},1,#{uid},#{fprice},#{fname},0)")
    Integer addsale(Sale sale);


    @Select("<script>"
            +"SELECT sid,num,datetime,fid,status,uid,fprice,fname,moneyStatus FROM sale   "
            +" <where>"
            +" status !=0"
            +"<if test='uid != null'> and uid=#{uid} </if> "
            +"<if test='datetime != null'> and datetime =#{datetime} </if> "
            +"</where>"
            +"order by  sid  desc"
            +"</script>"

    )
    List<Sale> selectsale (Sale sale);


    @Update("<script>"
            + "update sale"
            + "<set>"
            + " <if test='num != null'> num=#{num}, </if> "
            + " <if test='datetime != null'> datetime=#{datetime}, </if> "
            + " <if test='fid != null'> fid=#{fid}, </if> "
            + " <if test='status != null'> status=#{status}, </if> "
            + " <if test='moneyStatus != null'> moneyStatus=#{moneyStatus}, </if> "
            + "</set> "
            +" <where>"
            +"<if test='sid != null'> and sid=#{sid} </if> "
            +"<if test='datetime != null'> and datetime=#{datetime} </if> "
            +"</where>"
            + "</script>")

    Integer updatesale(Sale sale);


    @Select("SELECT DATETIME,fname,cate_name,num,fprice FROM sale s \n" +
            "LEFT JOIN \n" +
            "(SELECT cate_name,fid FROM food f \n" +
            "LEFT JOIN cate c ON f.mold= c.`cate_id`) fc ON fc.fid = s.`fid`   WHERE s.status != 0 and DATETIME > (SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 8 DAY),'%Y-%m-%d') )")
    List<Valume>  SevenSelectValume();

}
